import * as THREE from "../../three/build/three.module.js";

class MyMaterial{

    static AreaMaterial(aspect){
        
        let mat = new THREE.ShaderMaterial({
            uniforms:{
                _time:{value:1},
                _center:{value: new THREE.Vector2(121.47,31.20)},
                _aspect:{value:aspect},
                _useCircle:{value:false},
                _isTop:{value:false},
            },
            
            vertexShader:`
                varying vec2 vUv;
                varying vec3 vNormal;
                varying vec4 vProjectionPos;
                varying vec4 vWorldPos;
                varying float vFragDepth;

                void main(){
                    vUv = uv;
                    vNormal = normal;
                    vWorldPos = vec4(modelMatrix * vec4(position,1.)) ;
                    vProjectionPos = vec4(projectionMatrix * modelViewMatrix * vec4(position,1.)) ;
                    gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.);
                    vFragDepth = 1. + gl_Position.w;
                }
            `,
            fragmentShader:`
                #define BACKGROUNDCOL vec3(32,64,128)/256. * 0.7
                #define BACKGROUNDCOL2 vec3(32,128,128)/256. * 0.7
                #define POINTCOL vec3(256,256,256)/256. 
                #define SHANGHAICENTER vec2(121.490317, 31.222771)
                #define CIRCLECOL vec3(256,180,0)/256. 
                #define SPEED 0.3

                uniform float logDepthBufFC;

                uniform float _time;
                uniform vec2 _center;
                uniform float _aspect;
                uniform bool _useCircle;
                uniform bool _isTop;

                varying vec2 vUv;
                varying vec3 vNormal;
                varying vec4 vProjectionPos;
                varying vec4 vWorldPos;
                varying float vFragDepth;

                
                float Point(vec2 uv, float dis, float size){
                    size /= 2.;
                    float x = mod(uv.x/dis,1.)-.5;
                    float y = mod(uv.y/dis,1.)-.5;
                    return smoothstep(size,size-0.01, length(vec2(x,y)) );
                }

                float Circle(float l, float size){
                    return mod(pow(l*50.,0.3)+ _time * SPEED,1.);
                    l += _time * SPEED;
                    // l *= l;
                    return (1.-mod(l*10.,2.5))*.5;
                }

                void main(){
                    // 圆点背景
                    bool type = vNormal.z > 0.7; // 类型 true为正面, false为侧面
                    float point =  type ? Point(vec2(vWorldPos.x,vWorldPos.z), 0.05, 0.2) : 0.;  // worldPos 正面 计算点
                    vec3 background = mix(BACKGROUNDCOL,BACKGROUNDCOL2, vWorldPos.x - SHANGHAICENTER.x + 0.5);
                    gl_FragColor = vec4(mix(background, POINTCOL, point/2.) * vWorldPos.y,1.);

                    // 光环
                    if(_useCircle){
                        vec2 uv = vWorldPos.xz - vec2(_center.x,-_center.y);
                        float l = length(uv*2.);
                        float c = Circle(l, 0.) * .5;
                        c = max(c, 0.);
                        vec3 circleCol = CIRCLECOL * c;
                        gl_FragColor = gl_FragColor + vec4(circleCol,0.);
                    }
                    
                    gl_FragDepthEXT = _isTop ? log2( vFragDepth ) * 0.01 * 0.5 : log2( vFragDepth ) * logDepthBufFC * 0.5;
                    gl_FragColor = _isTop ? vec4(gl_FragColor.rgb + .2,1.) : gl_FragColor;
                    // gl_FragDepth = _isTop ? log2( vFragDepth ) * 0.01 * 0.5 : log2( vFragDepth ) * logDepthBufFC * 0.5;
                }
            `,
        });

        return mat;
    }


// 封藏 用最新的AreaMaterial 代替
/*     static CreateMaterial(aspect){
        
        let mat = new THREE.ShaderMaterial({
            uniforms:{
                _time:{value:1},
                _center:{value: new THREE.Vector2(121.47,31.20)},
                _aspect:{value:aspect}
            },
            
            vertexShader:`
                varying vec2 vUv;
                varying vec3 vNormal;
                varying vec4 vProjectionPos;
                varying vec4 vWorldPos;
                void main(){
                    vUv = uv;
                    vNormal = normal;
                    vWorldPos = vec4(modelMatrix * vec4(position,1.)) ;
                    vProjectionPos = vec4(projectionMatrix * modelViewMatrix * vec4(position,1.)) ;
                    gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.);

                }
            `,
            fragmentShader:`
                #define BACKGROUNDCOL vec3(32,64,128)/256. * 0.7
                #define BACKGROUNDCOL2 vec3(32,128,128)/256. * 0.7
                #define POINTCOL vec3(256,256,256)/256. 
                #define SHANGHAICENTER vec2(121.490317, 31.222771)
                #define CIRCLECOL vec3(256,180,0)/256. 
                #define SPEED 0.3

                uniform float _time;
                uniform vec2 _center;
                uniform float _aspect;

                varying vec2 vUv;
                varying vec3 vNormal;
                varying vec4 vProjectionPos;
                varying vec4 vWorldPos;

                
                float Point(vec2 uv, float dis, float size){
                    size /= 2.;
                    float x = mod(uv.x/dis,1.)-.5;
                    float y = mod(uv.y/dis,1.)-.5;
                    return smoothstep(size,size-0.01, length(vec2(x,y)) );
                }

                float Circle(float l, float size){
                    return mod(pow(l*50.,0.3)+ _time * SPEED,1.);
                    l += _time * SPEED;
                    // l *= l;
                    return (1.-mod(l*10.,2.5))*.5;
                }

                void main(){
                    // 圆点背景
                    bool type = vNormal.z > 0.7; // 类型 true为正面, false为侧面
                    float point =  type ? Point(vec2(vWorldPos.x,vWorldPos.z), 0.05, 0.2) : 0.;  // worldPos 正面 计算点
                    vec3 background = mix(BACKGROUNDCOL,BACKGROUNDCOL2, vWorldPos.x - SHANGHAICENTER.x + 0.5);
                    gl_FragColor = vec4(mix(background, POINTCOL, point/2.) * vWorldPos.y,1.);

                    // 光环
                    vec2 uv = vUv - _center;
                    float l = length(uv*2.);
                    float c = Circle(l, 0.) * .5;
                    c = max(c, 0.);
                    vec3 circleCol = CIRCLECOL * c;
                    gl_FragColor = gl_FragColor + vec4(circleCol,1.);
                }
            `,
        });

        return mat;
    }

    static PointMaterial(aspect){
        let mat = new THREE.ShaderMaterial({
            // depthTest:true, depthWrite:true,
            uniforms:{
                _aspect:{value:aspect}
            },
            
            vertexShader:`
                varying vec2 vUv;
                varying vec3 vNormal;
                varying vec4 vProjectionPos;
                varying vec4 vWorldPos;
                varying float vFragDepth;


                void main(){
                    vUv = uv;
                    vNormal = normal;
                    vProjectionPos = vec4(projectionMatrix * modelViewMatrix * vec4(position,1.)) ;
                    vWorldPos = vec4(modelMatrix * vec4(position,1.)) ;
                    gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.);

// gl_Position.z = (gl_Position.z - 1.0) * gl_Position.w;
                    vFragDepth = 1. + gl_Position.w;
                }
            `,
            fragmentShader:`

                #define BACKGROUNDCOL vec3(32,64,128)/256. * 0.7
                #define BACKGROUNDCOL2 vec3(32,128,128)/256. * 0.7
                #define POINTCOL vec3(256,256,256)/256. 
                #define SHANGHAICENTER vec2(121.490317, 31.222771)

                uniform float _aspect;
                uniform float logDepthBufFC;

                varying vec2 vUv;
                varying vec3 vNormal;
                varying vec4 vProjectionPos;
                varying vec4 vWorldPos;

                varying float vFragDepth;
                
                
                float Point(vec2 uv, float dis, float size){
                    size /= 2.;
                    float x = mod(uv.x/dis,1.)-.5;
                    float y = mod(uv.y/dis,1.)-.5;
                    return smoothstep(size,size-0.1, length(vec2(x,y)) );
                }
                void main(){
                    // vec2 ScreenPos = vec2(vProjectionPos / vProjectionPos.w) * vec2(_aspect,1.);
                    // float point = Point(ScreenPos.xy, 0.05, 0.2);  // 屏幕空间
                    // float point = Point(vUv, 0.05, 0.2);  // uv

                    bool type = vNormal.z > 0.7; // 类型 true为正面, false为侧面
                    float point =  type ? Point(vec2(vWorldPos.x,vWorldPos.z), 0.05, 0.2) : 0.;  // worldPos 正面 计算点

                    vec3 background = mix(BACKGROUNDCOL,BACKGROUNDCOL2, vWorldPos.x - SHANGHAICENTER.x + 0.5);
                    gl_FragColor = vec4(mix(background, POINTCOL, point/2.) * vWorldPos.y,1.);

                    gl_FragDepthEXT = log2( vFragDepth ) * logDepthBufFC * 0.5;

                    // gl_FragColor = vec4(1.);
                }
            `,
        });

        return mat;
    } */
}

export{MyMaterial}